#include <string.h>

#include "oled.h"
#include "oled_task.h"
#include "encoder_task.h"
#include "low_speed_task.h"
#include "device_app.h"
#include "ble_receive_task.h"
#include "ble_send_task.h"

#include "connect_wifi.h"
#include "tcp_client.h"

#include "ohos_init.h"
#include "cmsis_os2.h"

show_data_t show_data;

extern device_data_t device_data;
extern encoder_task_data_t encoder_task_data;
extern low_speed_task_data_t low_speed_task_data;

extern connect_wifi_data_t connect_wifi_data;
extern server_data_t server_data;
extern ble_receive_data_t ble_receive_data;
extern ble_send_data_t ble_send_data;

static void show_status_bar(void);

static void show_homepage(void);
static void show_homepage_start(void);
static void show_homepage_run(void);

static void show_select(void);
static void show_select_select(void);
static void show_select_run(void);

static void show_wifi(void);
static void show_wifi_start(void);
static void show_wifi_select(void);
static void show_wifi_run(void);

static void show_server(void);
static void show_server_start(void);
static void show_server_select(void);
static void show_server_run(void);

static void show_ble(void);
static void show_ble_start(void);
static void show_ble_select(void);
static void show_ble_run(void);

static void show_select_slave(void);
static void show_select_slave_start(void);
static void show_select_slave_select(void);
static void show_select_slave_run(void);

static void show_ctrl(void);
static void show_ctrl_start(void);
static void show_ctrl_select(void);
static void show_ctrl_run(void);
static void show_ctrl_run_power_ctrl(void);

static void show_setting(void);
static void show_setting_start(void);
static void show_setting_select(void);
static void show_setting_run(void);

static void show_back(void);

void oled_task(void *arg)
{
    (void) arg;
    show_data.show_num = SHOW_HOMEPAGE;
    show_data.show_homepage_state = START;
    while(1)
    {
        show_status_bar();    //状态栏显示
        switch(show_data.show_num)
        {
            case SHOW_HOMEPAGE : show_homepage();     break;    //主界面
            case SHOW_SELECT   : show_select();       break;    //一级菜单
            case SHOW_WIFI     : show_wifi();         break;    //wifi功能
            case SHOW_SERVER   : show_server();       break;
            case SHOW_BLE      : show_ble();          break;
            case SHOW_CTRL     : show_select_slave(); break;
            case SHOW_SETTING  : show_setting();      break;
            case SHOW_BACK     : show_back();         break;
            default : break;
        }
        osDelay(10);   //100ms
    }
}

void oled_task_creat(void)
{
    osThreadAttr_t attr;

    attr.name       = "oled_task";
    attr.attr_bits  = 0U;
    attr.cb_mem     = NULL;
    attr.cb_size    = 0U;
    attr.stack_mem  = NULL;
    attr.stack_size = 10240;
    attr.priority   = osPriorityNormal2;

    if (osThreadNew(oled_task, NULL, &attr) == NULL)
    {
        printf("[oled_task_creat] Falied to create oled_task!\n");
    }
}

APP_FEATURE_INIT(oled_task_creat);


/************************************************************************************************************************************************************
 *                                                               状态栏
 * **********************************************************************************************************************************************************/
static void show_status_bar(void)    //状态栏 WiFi，服务器，蓝牙，
{
    if(connect_wifi_data.last_wifi_connect_state != connect_wifi_data.wifi_connect_state)    //连接WiFi状态标志改变了才会改变图标的显示。
    {
        connect_wifi_data.last_wifi_connect_state = connect_wifi_data.wifi_connect_state;
        switch(connect_wifi_data.wifi_connect_state)
        {
            case 0: oled_clear_line(0, 0, 0, 17); bzero(device_data.stime, sizeof(device_data.stime)); break;      //清除WiFi图标 //清除服务器的图标，WiFi没有连接，服务器自然也连不上。更新服务器状态，连接上wifi立即连接
            case 1: OLED_Show_picture(0, 0, 8, 1); server_data.server_connect_state = 0; device_data.server_connect_immediate = 1; break;    //显示WiFi图标
            default : break;
        }
    }
    if(server_data.last_server_connect_state != server_data.server_connect_state)
    {
        server_data.last_server_connect_state = server_data.server_connect_state;
        switch(server_data.server_connect_state)
        {
            case 0: oled_clear_line(0, 0, 10, 20); bzero(device_data.stime, sizeof(device_data.stime)); break;        //清除服务器图标 //清除服务器的图标，WiFi没有连接，服务器自然也连不上，
            case 1: OLED_Show_picture(10, 0, 8, 2); break;       //显示服务器图标
            default : break;
        }
    }
    if(ble_receive_data.ble_num != ble_receive_data.ble_num_last)
    {
        ble_receive_data.ble_num_last = ble_receive_data.ble_num;

        if(ble_receive_data.ble_num >= 1)
        {
            OLED_Show_picture(20, 0, 8, 3);
        }
        else if(ble_receive_data.ble_num == 0)
        {
            oled_clear_line(0, 0, 20, 28);//清除蓝牙图标
        }
    }
}


/************************************************************************************************************************************************************
 *                                                               主界面
 * **********************************************************************************************************************************************************/
#if 1
static void show_homepage(void)//菜单显示，1主界面，时间，温度，湿度。
{
    switch(show_data.show_homepage_state)
    {
        case START : show_homepage_start(); break;
        case RUN : show_homepage_run(); break;
        default : break;
    }
}

static void show_homepage_start(void)
{
    if(device_data.oled_init_state == 1 && show_data.show_homepage_state == START)
    {
        oled_clear_line(2, 7, 0, 127);    //清除主屏幕的所有内容
        OLED_Show_Chinese(0, 2, 0);     //时
        OLED_Show_Chinese(16, 2, 1);    //间
        OLED_Show_Chinese(0, 4, 2);     //温
        OLED_Show_Chinese(16, 4, 4);    //度
        OLED_Show_Chinese(0, 6, 3);     //湿
        OLED_Show_Chinese(16, 6, 4);    //度
        show_data.show_homepage_state = RUN;
    }
}

static void show_homepage_run(void)
{
    uint8_t server_time[9] = {0};
    memcpy(server_time, device_data.stime, 9);
    uint8_t *pu8 = server_time+5;
    *pu8 = 0;
    OLED_Show_String(32, 2, 16, server_time);    //显示时间
    
    if(strcmp(low_speed_task_data.temperature, low_speed_task_data.last_temperature) != 0)
    {
        OLED_Show_String(32, 4, 16, (uint8_t*)low_speed_task_data.temperature);
        strcpy(low_speed_task_data.last_temperature, low_speed_task_data.temperature);
    }
    if(strcmp(low_speed_task_data.humidity, low_speed_task_data.last_humidity) != 0)
    {
        OLED_Show_String(32, 6, 16, (uint8_t*)low_speed_task_data.humidity);
        strcpy(low_speed_task_data.last_humidity, low_speed_task_data.humidity);
    }
}
#endif

/************************************************************************************************************************************************************
 *                                                               功能选择
 * **********************************************************************************************************************************************************/
#if 1
static void show_select(void)
{
    switch(show_data.show_select_state)
    {
        case SELECT : show_select_select(); break;
        case RUN : show_select_run(); break;
        default : break;
    }
}

static void show_select_select(void)
{
    uint8_t wifi[5] = {"wifi"};
    uint8_t server[7] = {"server"};
    if(show_data.show_select_num_last != show_data.show_select_num)
    {
        show_data.show_select_num_last = show_data.show_select_num;
        oled_clear_line(2, 7, 0, 127);    //清除主屏幕的所有内容
        switch(show_data.show_select_num)
        {
            case SHOW_HOMEPAGE : OLED_Show_picture(47, 2, 32, 0); OLED_Show_Chinese(47, 6, 5); OLED_Show_Chinese(64, 6, 6); break;    //主页
            case SHOW_WIFI     : OLED_Show_picture(47, 2, 32, 1); OLED_Show_String(47, 6, 16, wifi); break;    //wifi
            case SHOW_SERVER   : OLED_Show_picture(47, 2, 32, 2); OLED_Show_String(47, 6, 16, server); break;    //server
            case SHOW_BLE      : OLED_Show_picture(47, 2, 32, 3); OLED_Show_Chinese(47, 6, 7); OLED_Show_Chinese(64, 6, 8); break;    //蓝牙
            case SHOW_CTRL     : OLED_Show_picture(47, 2, 32, 4); OLED_Show_Chinese(47, 6, 9); OLED_Show_Chinese(64, 6, 10); break;    //控制
            case SHOW_SETTING  : OLED_Show_picture(47, 2, 32, 5); OLED_Show_Chinese(47, 6, 11); OLED_Show_Chinese(64, 6, 12); break;   //设置
            case SHOW_BACK     : OLED_Show_picture(47, 2, 32, 6); OLED_Show_Chinese(47, 6, 13); OLED_Show_Chinese(64, 6, 14); break;   //返回
            default : break;
        }
    }
}

static void show_select_run(void)
{
    show_data.show_num = show_data.show_select_num;
    switch(show_data.show_num)
    {
        case SHOW_HOMEPAGE : show_data.show_homepage_state = START; break;
        case SHOW_WIFI     : show_data.show_wifi_state = START; break;
        case SHOW_SERVER   : show_data.show_server_state = START; break;
        case SHOW_BLE      : show_data.show_ble_state = START; break;
        case SHOW_CTRL     : show_data.show_select_slave_state = START; break;  //show_data.show_ctrl_state = START;break;
        case SHOW_SETTING  : show_data.show_setting_state = START;break;
        case SHOW_BACK     : break;
        default            : break;
    }
}
#endif

/************************************************************************************************************************************************************
 *                                                               wifi
 * **********************************************************************************************************************************************************/
#if 1
static void show_wifi(void)
{
    switch(show_data.show_wifi_state)
    {
        case START : show_wifi_start(); break;
        case SELECT : show_wifi_select(); break;
        case RUN : show_wifi_run(); break;
        default : break;
    }
}

static void show_wifi_start(void)
{
    uint8_t wifi[5] = {"wifi"};
    oled_clear_line(2, 7, 0, 127);    //清除主屏幕的所有内容
    OLED_Show_picture(0, 2, 16, 2);    //箭头
    OLED_Show_Chinese(16, 2, 15); OLED_Show_Chinese(32, 2, 16);    //连接
    OLED_Show_String(48, 2, 16, wifi);
    OLED_Show_Chinese(16, 4, 17); OLED_Show_Chinese(32, 4, 18);    //断开
    OLED_Show_String(48, 4, 16, wifi);
    OLED_Show_Chinese(16, 6, 13); OLED_Show_Chinese(32, 6, 14);    //返回
    show_data.show_wifi_state = SELECT;
    show_data.show_wifi_num = 1;
}

static void show_wifi_select(void)
{
    if(show_data.show_wifi_num_last != show_data.show_wifi_num)
    {
        show_data.show_wifi_num_last = show_data.show_wifi_num;
        switch(show_data.show_wifi_num)
        {
            case 1 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 2, 16, 2); break;    //连接wifi
            case 2 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 4, 16, 2); break;    //断开wifi
            case 3 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 6, 16, 2); break;    //返回
            default : break;
        }
    }
}

static void show_wifi_run(void)
{
    uint8_t connecting[13] = {"connecting"};
    uint8_t disconnect[16] = {"disconnecting"};
    
    switch(show_data.show_wifi_num)
    {
        case 1 :  
        {
            OLED_Clear_Line(1);
            device_data.wifi_connect_flag = 1;
            if(connect_wifi_data.wifi_connect_state == 1)
            {
                uint8_t connected[20] = {"wifi connected"};
                OLED_Show_String(0, 1, 8, connected);
            }
            else if(connect_wifi_data.wifi_connect_state == 0)
            {
                OLED_Show_String(0, 1, 8, connecting); device_data.wifi_connect_immediate = 1;
            }
            show_data.show_wifi_state = SELECT;
            break;    //连接wifi
        }
        case 2 : 
        {
            OLED_Clear_Line(1);
            device_data.wifi_connect_flag = 0;
            if(connect_wifi_data.wifi_connect_state == 0)
            {
                uint8_t disconnected[20] = {"wifi disconnected"};
                OLED_Show_String(0, 1, 8, disconnected);
            }
            else if(connect_wifi_data.wifi_connect_state == 1)
            {
                OLED_Show_String(0, 1, 8, disconnect); 
            }
            show_data.show_wifi_state = SELECT;
        }break;    //断开wifi
        case 3 : show_back(); break;
        default : break;
    }
}
#endif

/************************************************************************************************************************************************************
 *                                                               服务器
 * **********************************************************************************************************************************************************/
#if 1
static void show_server(void)
{
    switch(show_data.show_server_state)
    {
        case START : show_server_start(); break;
        case SELECT : show_server_select(); break;
        case RUN : show_server_run(); break;
        default : break;
    }
}

static void show_server_start(void)
{
    oled_clear_line(2, 7, 0, 127);    //清除主屏幕的所有内容
    OLED_Show_picture(0, 2, 16, 2);    //箭头
    OLED_Show_Chinese(16, 2, 15); OLED_Show_Chinese(32, 2, 16);    //连接
    OLED_Show_Chinese(48, 2, 19); OLED_Show_Chinese(64, 2, 20); OLED_Show_Chinese(80, 2, 21);    //服务器
    OLED_Show_Chinese(16, 4, 17); OLED_Show_Chinese(32, 4, 18);    //断开
    OLED_Show_Chinese(48, 4, 19); OLED_Show_Chinese(64, 4, 20); OLED_Show_Chinese(80, 4, 21);    //服务器
    OLED_Show_Chinese(16, 6, 13); OLED_Show_Chinese(32, 6, 14);    //返回
    show_data.show_server_state = SELECT;
    show_data.show_server_num = 1;
}

static void show_server_select(void)
{
    if(show_data.show_server_num_last != show_data.show_server_num)
    {
        show_data.show_server_num_last = show_data.show_server_num;
        switch(show_data.show_server_num)
        {
            case 1 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 2, 16, 2); break;    //连接服务器
            case 2 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 4, 16, 2); break;    //断开服务器
            case 3 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 6, 16, 2); break;    //返回
            default : break;
        }
    }
}

static void show_server_run(void)
{
    uint8_t connecting[13] = {"connecting"};
    uint8_t disconnecting[16] = {"disconnecting"};

    switch(show_data.show_server_num)
    {
        case 1 : 
        {
            OLED_Clear_Line(1);
            if(server_data.server_connect_state == 1)
            {
                uint8_t connected[13] = {"connected"};
                OLED_Show_String(0, 1, 8, connected);
            }
            else if(server_data.server_connect_state == 0)
            {
                OLED_Show_String(0, 1, 8, connecting); device_data.server_connect_flag = 1; device_data.server_connect_immediate = 1;
            }
            show_data.show_server_state = SELECT;
        } break;    //连接wifi
        case 2 : 
        {
            OLED_Clear_Line(1);
            if(server_data.server_connect_state == 1)
            {
                OLED_Show_String(0, 1, 8, disconnecting); device_data.server_connect_flag = 0;
            }
            else if(server_data.server_connect_state == 0)
            {
                uint8_t wait4[16] = {"disconnected"};
                OLED_Show_String(0, 1, 8, wait4);
            }
            show_data.show_server_state = SELECT;
        } break;    //断开wifi
        case 3 : show_back(); break;
        default : break;
    }
}
#endif

/************************************************************************************************************************************************************
 *                                                               蓝牙
 * **********************************************************************************************************************************************************/
#if 1

static void show_ble(void)
{
    switch(show_data.show_ble_state)
    {
        case START  : show_ble_start(); break;
        case SELECT : show_ble_select(); break;
        case RUN    : show_ble_run(); break;
        default : break;
    }
}

static void show_ble_start(void)
{
    uint8_t restore[] = {"restore"};
    oled_clear_line(1, 7, 0, 127);    //清除主屏幕的所有内容
    OLED_Show_picture(0, 2, 16, 2);    //箭头
    OLED_Show_String(16, 2, 16, restore);    //恢复出厂设置
    OLED_Show_Chinese(16, 4, 15); OLED_Show_Chinese(32, 4, 16); OLED_Show_Chinese(48, 4, 7); OLED_Show_Chinese(64, 4, 8);//连接蓝牙
    OLED_Show_Chinese(16, 6, 13); OLED_Show_Chinese(32, 6, 14);    //返回
    show_data.show_ble_state = SELECT;
    show_data.show_ble_num = 1;
}

static void show_ble_select(void)
{
    uint8_t restoring[]       = {"restoring"};
    uint8_t connecting[]      = {"connecting"};
    uint8_t restored[]        = {"restored"};
	uint8_t connected[]       = {"connected"};
	uint8_t restore_timeout[] = {"restore timeout"};
	uint8_t connect_timeout[] = {"connect timeout"};
    if(show_data.show_ble_num_last != show_data.show_ble_num)
    {
        show_data.show_ble_num_last = show_data.show_ble_num;
        switch(show_data.show_ble_num)
        {
            case 1 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 2, 16, 2); break;    //恢复出厂设置
            case 2 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 4, 16, 2); break;    //连接蓝牙
            case 3 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 6, 16, 2); break;    //返回
            default : break;
        }
    }
    if(ble_receive_data.ble_state != ble_receive_data.ble_state_last)
    {
        ble_receive_data.ble_state_last = ble_receive_data.ble_state;
        switch(ble_receive_data.ble_state)
        {
            case RESTORED        : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, restored); break;
            case RESTORING       : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, restoring); break;
            case RESTORE_TIMEOUT : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, restore_timeout); break;    //恢复出厂设置超时
            // case NOT_PAIR        : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, restored); break;    //未配对
            // case PAIRED          : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, restored); break;    //与从机已配对，未连接
            case CONNECT_TIMEOUT : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, connect_timeout);break;    //连接超时
            case CONNECTING      : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, connecting); break;    //连接中
            case CONNECTED       : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, connected);break;    //已连接
            default : break;
        }
    }
}

static void show_ble_run(void)
{
    switch(show_data.show_ble_num)
    {
        case 1 : ble_send_data.ble_send_cmd_num = BLE_RESTORE; show_data.show_ble_state = SELECT; break;    //恢复出厂设置
        case 2 : ble_send_data.ble_send_cmd_num = BLE_CONNECT; show_data.show_ble_state = SELECT; break;    //设备入网
        case 3 : show_back(); break;
        default : break;
    }
}

#endif

/************************************************************************************************************************************************************
 *                                                               控制
 * **********************************************************************************************************************************************************/
#if 1
//先选择控制哪一个正常连接的控制端，然后再选择控制电源的开或者关。
static void show_select_slave(void)
{
    switch(show_data.show_select_slave_state)
    {
        case START  : show_select_slave_start(); break;
        case SELECT : show_select_slave_select(); break;
        case RUN    : show_select_slave_run(); break;
        default     : break;
    }
}

static void show_select_slave_start(void)
{
    uint8_t i = 0;
    uint8_t select_device[] = {"select device"};
    oled_clear_line(1, 7, 0, 127);    //清除主屏幕的所有内容
    OLED_Show_String(0, 1, 8, select_device);
    OLED_Show_picture(0, 2, 16, 2);    //箭头
    if(ble_receive_data.ble_num > 0)
    {
        for(i=0; i<ble_receive_data.ble_num; i++)//大于3个连接，先只显示3个，通过编码器选择其他的。
        {
            if(i == 3)break;
            if(ble_receive_data.ble_slave_id_data[i].ble_slave_state == ON_LINE)
            {
                OLED_Show_Num(16, i*2+2, i, 1);  //显示正常连接的设备的设备号
            }
        }
    }
    else
    {
        OLED_Clear_Line(1);
        uint8_t not_device[] = {"not device"};
        OLED_Show_String(0, 1, 8, not_device);
    }
    if(i<2)//还有位置显示返回
    {
        uint8_t back[] = {"back"};
        OLED_Show_String(16, i*2+2, 16, back);     //返回
    }
    show_data.show_select_slave_state = SELECT;
    show_data.show_select_slave_num = 1;
}

static void show_select_slave_select(void)
{
    if(show_data.show_select_slave_num_last != show_data.show_select_slave_num)
    {
        show_data.show_select_slave_num_last = show_data.show_select_slave_num;
        switch(show_data.show_select_slave_num)
        {
            case 1 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 2, 16, 2); break;
			case 2 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 4, 16, 2); break;
            case 3 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 6, 16, 2); break;
            default : break;
        }
    }
}

static void show_select_slave_run(void)
{
    if(show_data.show_select_slave_ok != 1)
    {
        show_data.show_select_slave_ok = 1;
        switch(show_data.show_select_slave_num)
        {
            case 1 : 
            switch(ble_receive_data.ble_num)
            {
                case 0 : show_data.show_select_slave_ok = 0; show_back(); break; 
                default : ble_send_data.slave_id[0] = ble_receive_data.ble_slave_id_data[0].ble_slave_id[0];
                          ble_send_data.slave_id[1] = ble_receive_data.ble_slave_id_data[0].ble_slave_id[1]; show_data.show_ctrl_state = START;break;
            }
             break;    //把选择的设备的id赋值到发送时的id变量中
            case 2 : 
            switch(ble_receive_data.ble_num)
            {
                case 1: show_data.show_select_slave_ok = 0; show_back(); break; 
                default : ble_send_data.slave_id[0] = ble_receive_data.ble_slave_id_data[1].ble_slave_id[0];
                          ble_send_data.slave_id[1] = ble_receive_data.ble_slave_id_data[1].ble_slave_id[1]; show_data.show_ctrl_state = START;break;
            }
            break;    //把选择的设备的id赋值到发送时的id变量中break;
            case 3 : show_data.show_select_slave_ok = 0; show_back(); break;    //返回 返回到菜单界面
            default : break;
        }
    }
    if(show_data.show_select_slave_ok == 1)
    {
        show_ctrl();
    }
}


static void show_ctrl(void)
{
    switch(show_data.show_ctrl_state)
    {
        case START : show_ctrl_start(); break;
        case SELECT : show_ctrl_select(); break;
        case RUN : show_ctrl_run(); break;
        default : break;
    }
}

static void show_ctrl_start(void)
{
    uint8_t power_on[] = {"power:ON"};
    uint8_t power_off[] = {"power:OFF"};
    uint8_t back[] = {"back"};
    oled_clear_line(1, 7, 0, 127);    //清除主屏幕的所有内容
    OLED_Show_picture(0, 2, 16, 2);    //箭头
    OLED_Show_String(16, 2, 16, power_off);    //关电源
	OLED_Show_String(16, 4, 16, power_on);    //开电源
	OLED_Show_String(16, 6, 16, back);     //返回
    show_data.show_ctrl_state = SELECT;
    show_data.show_ctrl_num = 1;
}

static void show_ctrl_select(void)
{
    uint8_t turning[] = {"Turning"};
    uint8_t succeed[] = {"succeed"};
    uint8_t failed[] = {"failed"};
    uint8_t timeout[] = {"timeout"};
    if(show_data.show_ctrl_num_last != show_data.show_ctrl_num)
    {
        show_data.show_ctrl_num_last = show_data.show_ctrl_num;
        switch(show_data.show_ctrl_num)
        {
            case 1 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 2, 16, 2); break;    //关电源
			case 2 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 4, 16, 2); break;    //开电源
            case 3 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 6, 16, 2); break;    //返回
            default : break;
        }
    }
    if(ble_send_data.ble_send_user_cmd_state != ble_send_data.ble_send_user_cmd_state_last)
    {
        ble_send_data.ble_send_user_cmd_state_last = ble_send_data.ble_send_user_cmd_state;
        switch(ble_send_data.ble_send_user_cmd_state)
        {
            case CMD_ING     : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, turning); break;
            case CMD_SUCCEED : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, succeed); break;
            case CMD_FAILED  : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, failed);  break;
            case CMD_TIMEOUT : OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, timeout); break;
            default : break;
        }
    }
}

static void show_ctrl_run(void)
{
    switch(show_data.show_ctrl_num)
    {
        case 1 : show_ctrl_run_power_ctrl(); break;    //关电源
        case 2 : show_ctrl_run_power_ctrl(); break;    //开电源
        case 3 : show_data.show_select_slave_state = START; show_data.show_select_slave_ok = 0; break;    //返回 返回到选择从机界面
        default : break;
    }
    show_data.show_ctrl_state = SELECT;
}

static void show_ctrl_run_power_ctrl(void)
{
    if(ble_receive_data.ble_num == 0)
    {
        OLED_Clear_Line(1);
        uint8_t not_device[] = {"not device"};
        OLED_Show_String(0, 1, 8, not_device);
    }
    else
    {
        switch(show_data.show_ctrl_num)
        {
            case 1 : ble_send_data.ble_send_cmd_num = BLE_SEND_POWER_OFF; break;    //关电源
            case 2 : ble_send_data.ble_send_cmd_num = BLE_SEND_POWER_ON; break;    //开电源
            default : break;
        }
    }
}


#endif

/************************************************************************************************************************************************************
 *                                                               设置
 * **********************************************************************************************************************************************************/
#if 1
static void show_setting(void)
{
    switch(show_data.show_setting_state)
    {
        case START  : show_setting_start(); break;
        case SELECT : show_setting_select(); break;
        case RUN    : show_setting_run(); break;
        default     : break;
    }
}

static void show_setting_start(void)
{
    uint8_t volume[] = {"volume"};
	uint8_t tone[] = {"tone"};
	uint8_t back[] = {"back"};
    OLED_Clear_All();    //清除主屏幕的所有内容
    OLED_Show_picture(0, 2, 16, 2);    //箭头
    OLED_Show_String(16, 2, 16, volume);
	OLED_Show_String(16, 4, 16, tone);    //定时
	OLED_Show_String(16, 6, 16, back);     //返回
    show_data.show_setting_state = SELECT;
    show_data.show_setting_num = 1;
}

static void show_setting_select(void)
{
    if(show_data.show_setting_num_last != show_data.show_setting_num)
    {
        show_data.show_setting_num_last = show_data.show_setting_num;
        switch(show_data.show_setting_num)
        {
            case 1 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 2, 16, 2); break;    //音量
			case 2 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 4, 16, 2); break;    //音调
            case 3 : oled_clear_line(2, 7, 0, 15); OLED_Show_picture(0, 6, 16, 2); break;    //返回
            default : break;
        }
    }
}

static void show_setting_run(void)
{
    switch(show_data.show_setting_num)
    {
        case 1 : show_data.show_setting_state = SELECT; break;    //恢复出厂设置
		case 2 : show_data.show_setting_state = SELECT; break;    //连接主机
        case 3 : show_back(); break;    //返回 返回到菜单界面
        default : break;
    }
}

#endif

/************************************************************************************************************************************************************
 *                                                               返回
 * **********************************************************************************************************************************************************/
static void show_back(void)
{
    oled_clear_line(1, 7, 0, 127);    //清除主屏幕的所有内容
    switch(show_data.show_num)
    {
        case SHOW_BACK : show_data.show_num = SHOW_HOMEPAGE; show_data.show_homepage_state = START; break;   //返回主界面
        default : show_data.show_num = SHOW_SELECT; show_data.show_select_state = SELECT; show_data.show_num_last = show_data.show_num - 1; break;    //返回选择界面
    }
}